package link.jfire.socket.test;

import java.nio.channels.ClosedChannelException;
import java.nio.charset.Charset;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import link.jfire.baseutil.collection.ByteCache;
import link.jfire.baseutil.simplelog.ConsoleLogFactory;
import link.jfire.baseutil.simplelog.Logger;
import link.jfire.baseutil.time.ThreadTimewatch;
import link.jfire.socket.socketclient.Client;
import link.jfire.socket.socketclient.listen.ClientChannelInfo;
import link.jfire.socket.socketclient.listen.GetReadResult;
import link.jfire.socket.socketserver.bus.BusinessCommand;
import link.jfire.socket.socketserver.exception.ConnectErrorException;

public class ClientMock
{
    public static void main(String[] args) throws InterruptedException
    {
        int testCount = 30;
        long total = 0;
        final Logger logger = ConsoleLogFactory.getLogger();
        final Charset charset = Charset.forName("utf-8");
        for (int test = 0; test < testCount; test++)
        {
            int size = 10;
            ExecutorService pool = Executors.newFixedThreadPool(size);
            final CountDownLatch countDownLatch = new CountDownLatch(size);
            for (int j = 0; j < size; j++)
            {
                pool.execute(new Runnable() {
                    
                    @Override
                    public void run()
                    {
                        Client clientUtil = new Client(new GetReadResult() {
                            @Override
                            public Object getRequestResult(ByteCache cache, byte command, byte result)
                            {
                                String str = cache.toString(charset);
                                return str;
                            }
                            
                            @Override
                            public void handlerException(Throwable e, ClientChannelInfo channelInfo)
                            {
                                if (e instanceof ClosedChannelException)
                                {
                                    
                                }
                                else
                                {
                                    logger.error("通道{}发生异常", channelInfo.getAddress(), e);
                                }
                            }
                            
                            @Override
                            public void receivePushMsg(ByteCache cache, byte command, byte result)
                            {
                                // TODO Auto-generated method stub
                                
                            }
                            
                        }).setIp("192.168.40.114").setPort(81);
                        ByteCache cache = new ByteCache();
                        String str = "你好啊";
                        cache.clear();
                        cache.putArray(str.getBytes(charset));
                        countDownLatch.countDown();
                        try
                        {
                            countDownLatch.await();
                        }
                        catch (InterruptedException e2)
                        {
                            // TODO Auto-generated catch block
                            e2.printStackTrace();
                        }
                        try
                        {
                            for (int i = 0; i < 10000; i++)
                            {
                                clientUtil.sendData(BusinessCommand.ECHO, cache);
                                cache.startRead(0);
                            }
                            cache.startRead(0);
                            Future<?> future = clientUtil.sendData(BusinessCommand.ECHO, cache);
                            future.get();
                            clientUtil.close();
                        }
                        catch (ConnectErrorException | InterruptedException | ExecutionException e1)
                        {
                            e1.printStackTrace();
                        }
                    }
                });
            }
            ThreadTimewatch.start();
            pool.shutdown();
            pool.awaitTermination(50, TimeUnit.SECONDS);
            ThreadTimewatch.end();
            System.out.println(ThreadTimewatch.getTotalTime());
            if (test > 0)
            {
                total += ThreadTimewatch.getTotalTime();
            }
        }
        System.out.println("平均耗时：" + (total) / testCount);
    }
}
